package com.judge.pe.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import com.judge.pe.R;

/**
 * Created by xuzhifan on 2015/9/14.
 */
public class LinearLayoutWithBorder extends LinearLayout{
    private Paint mPaint;
    private Path mPath;
    private boolean drawLeftBorder;
    private boolean drawRightBorder;
    private boolean drawBottomBorder;
    private boolean drawTopBorder;
    private int borderColor;

    public LinearLayoutWithBorder(Context context) {
        super(context);
        setWillNotDraw(false);
    }

    public LinearLayoutWithBorder(Context context, AttributeSet attrs){
        super(context, attrs);
        setWillNotDraw(false);
        mPaint = new Paint();
        mPath = new Path();
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LinearLayoutWithBorder);
        drawBottomBorder = a.getBoolean(R.styleable.LinearLayoutWithBorder_drawBottomBorder,true);
        drawTopBorder = a.getBoolean(R.styleable.LinearLayoutWithBorder_drawTopBorder,true);
        drawLeftBorder = a.getBoolean(R.styleable.LinearLayoutWithBorder_drawLeftBorder,true);
        drawRightBorder = a.getBoolean(R.styleable.LinearLayoutWithBorder_drawRightBorder,true);
        borderColor = a.getColor(R.styleable.LinearLayoutWithBorder_borderColor,R.color.font_gray_light);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(borderColor);
    }

    public LinearLayoutWithBorder(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(drawLeftBorder)drawLeftBorder();
        if(drawRightBorder)drawRightBorder();
        if(drawTopBorder)drawTopBorder();
        if(drawBottomBorder)drawBottomBorder();
        canvas.drawPath(mPath,mPaint);
    }
    private void drawLeftBorder(){
        mPath.moveTo(1,1);
        mPath.lineTo(1,this.getHeight()-1);
    }
    private void drawRightBorder(){
        mPath.moveTo(this.getWidth()-1,1);
        mPath.lineTo(this.getWidth()-1,this.getHeight()-1);
    }
    private void drawTopBorder(){
        mPath.moveTo(1,1);
        mPath.lineTo(this.getWidth()-1,1);
    }
    private void drawBottomBorder(){
        mPath.moveTo(1,this.getHeight()-1);
        mPath.lineTo(this.getWidth()-1,this.getHeight()-1);
    }
}
